package com.mach.platform.service;

import com.google.common.collect.Lists;
import com.mach.platform.domain.ProductionState;
import com.mach.platform.repository.ProductionStateRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class ProductionStateServiceImpl implements BaseService<ProductionState>{

    @Autowired
    private ProductionStateRepo productionStateRepo;

    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public ProductionState save(ProductionState productionState) {
        return null;
    }

    @Override
    public List<ProductionState> insertObjInBatch(List<ProductionState> list) {
        return null;
    }

    @Override
    public ProductionState update(ProductionState productionState) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return productionStateRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<ProductionState> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (ObjectUtils.allNotNull(startTime, endTime)) {
                LocalDateTime ldtStartTime = LocalDateTime.parse(startTime.toString(), df);
                LocalDateTime ldtEndTime = LocalDateTime.parse(endTime.toString(), df);
                Page<ProductionState> result = productionStateRepo.getListByTime(ldtStartTime, ldtEndTime, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public ProductionState info(Long id) {
        return null;
    }

    public void add(List<ProductionState> productionStateList) {
        if (productionStateList != null && productionStateList.size() > 0){
            List<ProductionState> newList = Lists.newArrayList();
            productionStateList.forEach(productionState -> {
                if (StringUtils.isNotBlank(productionState.getProductNameRate())){
                    if (productionState.getProductionStateTime() == null) {
                        productionState.setProductionStateTime(LocalDateTime.now());
                    }
                    newList.add(productionState);
                }
            });
            productionStateRepo.saveAll(newList);
        }
    }

    public void update(List<ProductionState> productionStateList) {
        this.add(productionStateList);
    }

    public List<ProductionState> getProductionStateByTime(String time) {
        if (StringUtils.isNotBlank(time)){
            List<ProductionState> result = productionStateRepo.getProductionStateByTime(time);
            return result;
        }
        return null;
    }
}
